﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgramMain.Database;
using ProgramMain.Database.SimpleOpticNetDBTableAdapters;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class DocumentList : ProgramMain.Framework.BaseGridListCtl
    {
        public DocumentList()
        {
            InitializeComponent();
        }

        public int? TagID { get; set; }
        public int? VertexID { get; set; }
        public int? CableTunnelID { get; set; }
        public int? CurveElementID { get; set; }

      public SimpleOpticNetDB.DocumentRow Document
        {
            get
            {
                if(dataSource.Current != null)
                   return (SimpleOpticNetDB.DocumentRow)((DataRowView)dataSource.Current).Row;
                return null;
            }
        }
    
        protected override void LoadData()
        {
            this.documentTableAdapter.ClearBeforeFill = true;
            this.documentTableAdapter.Fill(this.simpleOpticNetDB.Document, TagID, VertexID, CableTunnelID,
                                           CurveElementID, DetailID);
            UpdateStatusText(this.simpleOpticNetDB.Document);
        }

        protected override void UpdateData()
        {
            //внести изменения в базу
            this.documentTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.documentTableAdapter.Update(this.simpleOpticNetDB.Document);
            UpdateStatusText(this.simpleOpticNetDB.Document);
        }

        public void AddDocument()
        {
            if (!CheckSecurityEditAction()) return;
            DocumentDetail dialog = new DocumentDetail();
            dialog.dataSource = dataSource;
            dialog.dataSource.AddNew();
            SimpleOpticNetDB.DocumentRow row =
                (SimpleOpticNetDB.DocumentRow) ((DataRowView) dialog.dataSource.Current).Row;
            if (TagID.HasValue) row["TagID"] = TagID;
            if (VertexID.HasValue) row["VertexID"] = VertexID;
            if (CableTunnelID.HasValue) row["CableTunnelID"] = CableTunnelID;
            if (CurveElementID.HasValue) row["CurveElementID"] = CurveElementID;
            DialogResult dlgRes = dialog.ShowModal(BaseModalDetailCtl.ShowModeTypes.Add);
            if (dlgRes == DialogResult.OK)
            {
                //dataSource.EndEdit();
                this.documentTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
                this.documentTableAdapter.Update(this.simpleOpticNetDB.Document);
                if (row.ID > 0)
                    new FileAdapter().Document_UpLoad(row.ID, dialog.content);
            }
        }

        public byte[] GetDocumentContent(int docID)
        {
                object o = new FileAdapter().Document_DownLoad(docID);
                if(o is byte[])
                    return (byte[]) o;
            return null;
        }

        override protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            return new DocumentDetail();
        }
        public event EventHandler DocumentChanged;
        private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (DocumentChanged != null) DocumentChanged(this, EventArgs.Empty);
        }
    }
}